package com.navi.mould1.service.impl;

import com.navi.bean.in.ConditionBean;
import com.navi.bean.out.pmTask_report.PmTaskReportGridOut;
import com.navi.bean.sql.SqlVo;
import com.navi.mould1.service.IPmTaskReportService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;
import java.util.List;

import static com.navi.bean.sql.SqlVo.jointInSql;
import static com.navi.core.util.JdbcUtils.mySql_QueryRunner_QueryListBySql;

@Slf4j
@Service
public class PmTaskReportServiceImpl implements IPmTaskReportService {


    public List<PmTaskReportGridOut> getGridData(ConditionBean conditions) {
        final SqlVo sqlVo = new SqlVo();
        if (StringUtils.isEmpty(conditions.getDetail_flag())){
            sqlVo.appendSql("SELECT");
            sqlVo.appendSql("b_1.data_desc AS fab_id,");
            sqlVo.appendSql("A.fab_id_fk,");
            sqlVo.appendSql("b_3.data_desc AS line_id,");
            sqlVo.appendSql("A.line_id_fk,");
            sqlVo.appendSql("b_2.tool_dsc AS tool_id,");
            sqlVo.appendSql("A.tool_id_fk,");
            sqlVo.appendSql("A.shift_id_fk,");
            sqlVo.appendSql("A.pm_stat,");
            sqlVo.appendSql("A.pm_type AS pm_type_fk,");
            sqlVo.appendSql("A.from_date,");
            sqlVo.appendSql("A.to_date,");
            sqlVo.appendSql("A.ac_pm_beg_timestamp,");
            sqlVo.appendSql("A.ac_pm_end_timestamp");
            sqlVo.appendSql("FROM icim_jn.ret_tool_pm A");
            sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from bis_data where data_cate = 'FBID') b_1 ON A.fab_id_fk = b_1.data_ext");
            sqlVo.appendSql("LEFT JOIN (select tool_id,tool_dsc from bis_tool) b_2 ON A.tool_id_fk = b_2.tool_id");
            sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from bis_data where data_cate = 'AREA') b_3 ON A.line_id_fk = b_3.data_ext");
            sqlVo.appendSql("WHERE 1=1");
            if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND A.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
            if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND A.LINE_ID_FK in" + jointInSql(conditions.getBay_id()));
            if (StringUtils.isNotEmpty(conditions.getOpe_id())) sqlVo.appendSql("AND A.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where root_tool_id in (select tool_id_fk from bis_tool_gr where toolg_id in (select toolg_id_fk from icim_jn.bis_ope where ope_id in" + jointInSql(conditions.getOpe_id()) + ")))");
            if (StringUtils.isNotEmpty(conditions.getToolg_id())) sqlVo.appendSql("AND A.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where root_tool_id in (select tool_id_fk from bis_tool_gr where toolg_id in " + jointInSql(conditions.getToolg_id()) + "))");
            if (StringUtils.isNotEmpty(conditions.getTool_type())) sqlVo.appendSql("AND A.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where unit_typ in" + jointInSql(conditions.getTool_type()) + ")");
            if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND A.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
            if (StringUtils.isNotEmpty(conditions.getShift_id())) sqlVo.appendSql("AND A.SHIFT_ID_FK in" + jointInSql(conditions.getShift_id()));
            if (StringUtils.isNotEmpty(conditions.getPm_type())) sqlVo.appendSql("AND A.PM_TYPE in" + jointInSql(conditions.getPm_type()));
            if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.FROM_DATE >= ? ", conditions.getStart_time());
            if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.FROM_DATE <= ? ", conditions.getEnd_time() + " 23:59:59");
            sqlVo.appendSql("ORDER BY fab_id, line_id, tool_id, shift_id_fk desc, from_date");
        }else if ("true".equals(conditions.getDetail_flag())){
            sqlVo.appendSql("SELECT");
            sqlVo.appendSql("E.data_dsc as item_name,");
            sqlVo.appendSql("E.u_spec,");
            sqlVo.appendSql("E.l_spec,");
            sqlVo.appendSql("F.evt_seq_id as item_task_stat,");
            sqlVo.appendSql("F.data_value,");
            sqlVo.appendSql("H.usr_name AS evt_usr,");
            sqlVo.appendSql("G.usr_name AS maintenance_man,");
            sqlVo.appendSql("b_1.data_desc AS fab_id,");
            sqlVo.appendSql("A.fab_id_fk,");
            sqlVo.appendSql("b_3.data_desc AS line_id,");
            sqlVo.appendSql("A.line_id_fk,");
            sqlVo.appendSql("b_2.tool_dsc AS tool_id,");
            sqlVo.appendSql("A.tool_id_fk,");
            sqlVo.appendSql("A.shift_id_fk,");
            sqlVo.appendSql("A.pm_stat,");
            sqlVo.appendSql("A.pm_type AS pm_type_fk,");
            sqlVo.appendSql("A.from_date,");
            sqlVo.appendSql("A.to_date,");
            sqlVo.appendSql("A.ac_pm_beg_timestamp,");
            sqlVo.appendSql("A.ac_pm_end_timestamp");
            sqlVo.appendSql("FROM icim_jn.ret_tool_pm A");
            sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from bis_data where data_cate = 'FBID') b_1 ON A.fab_id_fk = b_1.data_ext");
            sqlVo.appendSql("LEFT JOIN (select tool_id,tool_dsc from bis_tool) b_2 ON A.tool_id_fk = b_2.tool_id");
            sqlVo.appendSql("LEFT JOIN (select data_ext,data_desc from bis_data where data_cate = 'AREA') b_3 ON A.line_id_fk = b_3.data_ext");
            sqlVo.appendSql("LEFT JOIN icim_jn.ret_tool_pm_item E ON A.pm_item_id = E.pm_item_id_fk");
            sqlVo.appendSql("LEFT JOIN icim_jn.ret_tool_pm_list F ON A.evt_seq_id = F.pm_evt_seq_id AND E.data_dsc = SUBSTRING_INDEX(F.data_dsc, ') ', -1)");
            sqlVo.appendSql("LEFT JOIN bis_user G ON F.maintenance_man = G.usr_id");
            sqlVo.appendSql("LEFT JOIN bis_user H ON A.evt_usr = H.usr_id");
            sqlVo.appendSql("WHERE 1=1");
            if (StringUtils.isNotEmpty(conditions.getFab_id())) sqlVo.appendSql("AND A.FAB_ID_FK in" + jointInSql(conditions.getFab_id()));
            if (StringUtils.isNotEmpty(conditions.getBay_id())) sqlVo.appendSql("AND A.LINE_ID_FK in" + jointInSql(conditions.getBay_id()));
            if (StringUtils.isNotEmpty(conditions.getOpe_id())) sqlVo.appendSql("AND A.TOOL_ID_FK in(select tool_id from icim_jn.bis_tool where root_tool_id in (select tool_id_fk from bis_tool_gr where toolg_id in (select toolg_id_fk from icim_jn.bis_ope where ope_id in" + jointInSql(conditions.getTool_id()) + ")))");
            if (StringUtils.isNotEmpty(conditions.getTool_id())) sqlVo.appendSql("AND A.TOOL_ID_FK in" + jointInSql(conditions.getTool_id()));
            if (StringUtils.isNotEmpty(conditions.getShift_id())) sqlVo.appendSql("AND A.SHIFT_ID_FK in" + jointInSql(conditions.getShift_id()));
            if (StringUtils.isNotEmpty(conditions.getPm_type())) sqlVo.appendSql("AND A.PM_TYPE in" + jointInSql(conditions.getPm_type()));
            if (StringUtils.isNotEmpty(conditions.getStart_time())) sqlVo.appendSqlAndAddParam("AND A.FROM_DATE >= ? ", conditions.getStart_time());
            if (StringUtils.isNotEmpty(conditions.getEnd_time())) sqlVo.appendSqlAndAddParam("AND A.FROM_DATE <= ? ", conditions.getEnd_time() + " 23:59:59");
            sqlVo.appendSql("ORDER BY fab_id, line_id, tool_id, shift_id_fk desc, from_date");
        }

        List<PmTaskReportGridOut> resultDataList = mySql_QueryRunner_QueryListBySql(sqlVo, PmTaskReportGridOut.class);

        resultDataList.forEach(data -> {
            String shift = "";
            switch (data.getShift_id_fk()==null?"":data.getShift_id_fk()) {
                case "D": shift = "白班"; break;
                case "N": shift = "夜班"; break;
            }
            data.setShift_id(shift);

            String pm_type = "";
            switch (data.getPm_type_fk()==null?"":data.getPm_type_fk()) {
                case "W" : pm_type = "周保养"; break;
                case "2W": pm_type = "双周保养"; break;
                case "M" : pm_type = "月保养"; break;
                case "Q" : pm_type = "季保养"; break;
                case "6M": pm_type = "半年保养"; break;
                case "Y" : pm_type = "年保养"; break;
            }
            data.setPm_type(pm_type);

            String pm_stat = null;
            switch (data.getPm_stat()) {
                case "COMP" : pm_stat = "完成"; break;
                case "WAIT" : pm_stat = "等待"; break;
                case "INPR" : pm_stat = "进行中"; break;
                case "DELAY": pm_stat = "未到期"; break;
                case "UNUS": pm_stat = "异常结束"; break;
            }
            data.setPm_stat(pm_stat);

            //等待状态不显示操作人
            if ("等待".equals(data.getPm_stat())) {
                data.setEvt_usr("");
            }

            //如果保养操作人为空，则取保养计划操作人
            if (StringUtils.isEmpty(data.getMaintenance_man())) {
                data.setMaintenance_man(data.getEvt_usr());
            }
        });

        return resultDataList;
    }

}